package com.imau.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imau.bean.Role;
import com.imau.bean.User;
import com.imau.bean.UserRole;
import com.imau.mapper.UserMapper;
import com.imau.mapper.UserRoleMapper;
import com.imau.service.UserService;
import org.apache.ibatis.executor.BatchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private  UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Override
    public boolean save(User user) {

        //1.角色中间表维护
        //1.1 用户角色在中间表中删掉

        //2.保存其他属性信息
        int insert = userMapper.insert(user);

        //1.2 用户角色在中间表中添加
        boolean b = this.assignRoles(user.getId(), user.getRoles());
        return b && insert > 0;
    }

    @Override
    public boolean removeById(String id) {
        int i = userMapper.deleteById(id);

        boolean b = unassignRoles(id);
        return  i>0 && b;
    }

    @Override
    public User findById(String id) {
        return userMapper.selectById(id);
    }

    @Override
    public User findByField(User user) {

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(user.getPassword()),User::getPassword,user.getPassword());
        wrapper.eq(StringUtils.isNotEmpty(user.getUserName()),User::getUserName,user.getUserName());
        wrapper.eq(StringUtils.isNotEmpty(user.getMobile()),User::getMobile,user.getMobile());
        wrapper.eq(StringUtils.isNotEmpty(user.getStatus()),User::getStatus,user.getStatus());
       // wrapper.between(StringUtils.checkValNotNull(user.getCreateTime()),User::getCreateTime,user.getCreateTime());

        return userMapper.selectOne(wrapper);
    }

    @Override
    public User findByMobile(String mobile) {
        return null;
    }

    @Override
    public IPage<User> queryUserList(User user, Integer pageNum, Integer pageSize) {

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(user.getUserName()),User::getUserName,user.getUserName());
        wrapper.like(StringUtils.isNotEmpty(user.getMobile()),User::getMobile,user.getMobile());
        wrapper.eq(StringUtils.isNotEmpty(user.getStatus()),User::getStatus,user.getStatus());
wrapper.between(StringUtils.checkValNotNull(user.getParams().get("biginTime")) && StringUtils.checkValNotNull(user.getParams().get("endTime")),
        User::getCreateTime,user.getParams().get("biginTime"),user.getParams().get("endTime"));

        IPage<User> page = new Page<>(pageNum,pageSize);

        return userMapper.selectPage(page, wrapper);
    }

    @Override
    public boolean assignRoles(String uid, Set<Role> roles) {

        List<UserRole> userRoles = new ArrayList<>();
        for (Role role : roles) {
            userRoles.add(new UserRole(role.getId(),uid));
        }

        List<BatchResult> insert = userRoleMapper.insert(userRoles);


        return insert!=null;
    }

    @Override
    public boolean assignRoles(String uid, String rids[]) {
  List<UserRole> userRoles = new ArrayList<>();

        for (String rid : rids) {
            userRoles.add(new UserRole(rid,uid));
        }

        List<BatchResult> insert = userRoleMapper.insert(userRoles);


        return insert!=null;
    }
    @Override
    public boolean unassignRoles(String uid) {

        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("user_id",uid);
        int rows = userRoleMapper.deleteByMap(columnMap);
        return rows > 0;
    }

    @Override
    public boolean editUser(User user) {
        //1.角色中间表维护
        //1.1 用户角色在中间表中删掉
        //1.2 用户角色在中间表中添加
        boolean b1 = this.unassignRoles(user.getId());
        boolean b = this.assignRoles(user.getId(), user.getRoles());

        int i = userMapper.updateById(user);
        return i > 0;
    }

    @Override
    public User checkLogin(String mobile, String password) {

       User user = new User();
       user.setPassword(password);
       user.setMobile(mobile);
        return this.findByField(user);
    }
}
